﻿using AutoMapper;
using MediatR;
using SmartTracking.API.Write.Application.Commands.PoultryHouse_command;
using SmartTracking.Domain.Sheep;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.PoultryHouse_handler
{
    public class AddPoultryHouseCommandHandler:IRequestHandler<AddPoultryHouseCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<PoultryHouse> houseRep;
        private readonly IMapper mapper;

        public AddPoultryHouseCommandHandler(
            IBaseRepository<PoultryHouse> houseRep,
            IMapper mapper)
        {
            this.houseRep = houseRep;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(AddPoultryHouseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.新增成功;
            result.Message = ApiEnum.新增成功.ToString();

            PoultryHouse house = mapper.Map<PoultryHouse>(request);
            house.CreateTime = DateTime.Now;
            house.IsDeleted = false;
            var res = await houseRep.AddAsync(house);

            result.data = res;
            return await Task.FromResult(result);
        }
    }
}
